/*
 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * Version 2.0.1
 * Demo: http://www.texotela.co.uk/code/jquery/newsticker/
 *
 * $LastChangedDate$
 * $Rev$
 *
 * A basic news ticker.
 * @name     newsticker (or newsTicker)
 * @param    delay      Delay (in milliseconds) between iterations. Default 4 seconds (4000ms)
 * @author   Sam Collett (http://www.texotela.co.uk)
 * @example  $("#news").newsticker(); // or $("#news").newsTicker(5000);
 */
 
(function($) {
$.fn.newsTicker = $.fn.newsticker = function(delay){
	delay = delay || 3000;
	initTicker = function(el){
		el.items = $("li", el);
		el.items.not(":eq(0)").hide().end();
		el.currentitem = 0;
		startTicker(el);
	};
	startTicker = function(el){
		el.tickfn = setInterval(function() { doTick(el) }, delay)
	};
	doTick = function(el){
		if(el.pause) return;
		$.newsticker.pause(el);
		$(el.items[el.currentitem]).fadeOut("slow",
			function(){
				$(this).hide();
				el.currentitem = ++el.currentitem % (el.items.size());
				$(el.items[el.currentitem]).fadeIn("slow",
					function(){
						$.newsticker.resume(el);
					}
				);
			}
		);
	};
	this.each(
		function(){
			if(this.nodeName.toLowerCase()!= "ul") return;
			initTicker(this);
		}
	)
	.addClass("newsticker")
	.hover(
		function(){
			$.newsticker.pause(this);
		},
		function(){
			$.newsticker.resume(this);
		}
	);
	return this;
};

$.newsticker = $.newsTicker = {
	pause: function(el){
		(el.jquery ? el[0] : el).pause = true;
	},
	resume: function(el){
		(el.jquery ? el[0] : el).pause = false;
	},
	clear: function(el){
		el = (el.jquery ? el[0] : el);
		clearInterval(el.tickfn);
		delete el.tickfn;
		delete el.items;
		delete el.currentItem;
	}
}
})(jQuery);